package com.afkt.project.ui.adapter;


import com.afkt.project.model.bean.ItemBean;
import com.ohos.afkt.ResourceTable;
import com.dev.base.widget.BaseImageView;
import com.dev.base.widget.BaseTextView;
import com.dev.glide.utils.OhosGlide;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.*;


import java.io.IOException;
import java.util.List;

public class PagerSnapAdapter extends BaseItemProvider {

    private List<ItemBean> dataList;

    private AbilitySlice mContext;

    public PagerSnapAdapter(List<ItemBean> dataList, AbilitySlice context) {
        this.dataList = dataList;
        mContext = context;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component rootView;
        ViewHolder viewHolder;
        if (component == null) {
            viewHolder = new ViewHolder();
            component = viewHolder.rootView;
            component.setTag(viewHolder);
            rootView = viewHolder.rootView;
        } else {
            viewHolder = (ViewHolder) component.getTag();
            rootView = viewHolder.rootView;
        }
        viewHolder.subTtitle.setText(dataList.get(i).subtitle);
        viewHolder.title.setText(dataList.get(i).title);

        try {
            OhosGlide.with(mContext).load(dataList.get(i).imageUrl).
                    def(ResourceTable.Media_icon).into(viewHolder.image);
        } catch (IOException e) {
            e.printStackTrace();
        }
        viewHolder.time.setText(" " + dataList.get(i).timeFormat);

        return rootView;
    }


    class ViewHolder {

        private Component rootView;

        private BaseTextView title;

        private BaseTextView subTtitle;

        private BaseImageView image;

        //private BaseTextView content;

        private BaseTextView time;

        private BaseTextView page;

        public ViewHolder() {
            rootView =  LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_adapter_pager_snap, null, false);
            title = (BaseTextView) rootView.findComponentById(ResourceTable.Id_vid_ags_title_tv);
            subTtitle = (BaseTextView) rootView.findComponentById(ResourceTable.Id_vid_ags_subtitle_tv);
            time = (BaseTextView) rootView.findComponentById(ResourceTable.Id_vid_ags_time_tv);
            page = (BaseTextView) rootView.findComponentById(ResourceTable.Id_vid_ags_index_tv);
            image = (BaseImageView) rootView.findComponentById(ResourceTable.Id_vid_ags_igview);
        }
    }
}
